<?php

namespace Services\RabbitMq;

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

class RabbitMqSender extends BasicSender
{
    public function createConnection($host, $port, $user, $password)
    {
        $this->connection = new AMQPStreamConnection($host, $port, $user, $password);
        $this->channel = $this->connection->channel();
    }

    public function changeQueue(string $queueName)
    {
        $this->activeQueue = $queueName;
    }

    public function sendMessage(string $message, string $key, string $messageType = 'JSON')
    {
        $msg = new AMQPMessage($message);
        $this->channel->basic_publish($msg, '', $key);
    }

    public function close()
    {
        $this->channel->close();
        $this->connection->close();
    }

    public function __destruct()
    {
        $this->channel->close();
        $this->connection->close();
    }
}
